#include "datastruct.h"

#include <sys/ipc.h>
#include <sys/shm.h>

#define EXIT -1
#define INFO 1
#define DEBUG 2
#define WARNING 3
#define ERR 4
#define GETUSER 6
#define GETSTAT 9

#define BUFFSIZE 20

int global = 3;

typedef struct _levelHandler{
    int t_level;
    void (*pfun)(int, void *);
} Level_handler;

int main()
{
    int shmid;
    key_t key;
    int *shmaddr = NULL;

    key = ftok("./", 802);
    if(key < 0)
        err_sys("ftok error");
    
    shmid = shmget(key, BUFFSIZE, IPC_CREAT | 0666);
    if(shmid < 0)
        err_sys("shmget error");

    shmaddr = shmat(shmid, NULL, 0);
    printf("shmaddr addr : %p\n", shmaddr);
    if(shmaddr < 0)
        err_sys("shmat error");

	*shmaddr = global;

	printf("before input global %d\n", global);
	int lev_temp;
	while(1)
	{
		printf("Please input the level [INFO(1), DEBUG(2), WARNING(3), ERR(4)] : \n");
		scanf("%d", &lev_temp);
		switch(lev_temp)
		{
			case EXIT : 
			{
				printf("It is EXIT level.\n");
				global = -1;
				*shmaddr = global;
				break;
			}
			case INFO : 
			{
				printf("It is INFO level.\n");
				global = 1;
				*shmaddr = global;
				break;
			}
			case DEBUG : 
			{
				printf("It is DEBUG level.\n");
				global = 2;
				*shmaddr = global;
				break;
			}
			case WARNING : 
			{
				printf("It is WARNING level.\n");
				global = 3;
				*shmaddr = global;
				break;
			}
			case ERR : 
			{
				printf("It is ERR level.\n");
				global = 4;
				*shmaddr = global;
				break;
			}
			case GETUSER : 
			{
				printf("It is GETUSER level.\n");
				global = 6;
				*shmaddr = global;
				break;
			}
			case GETSTAT : 
			{
				printf("It is GETSTAT level.\n");
				global = 9;
				*shmaddr = global;
				break;
			}
		}	
	}

	printf("after global %d\n", global);
	printf("%d\n", *shmaddr);

	while(1);//用于测试等待

    return 0;
}
